<?php

class Router
{
    protected $workers = array();

    public function registerWorker($name, $class, $path)
    {
        $this->workers[$name] = array(
            'path' => $path,
            'class' => $class,
        );
    }

    public function dispatch($arguments)
    {
        $script = array_shift($arguments);
        $workerName = array_shift($arguments);

        if (!isset($this->workers[$workerName]))
            throw new Exception('Unknown worker: ' . $workerName);

        include($this->workers[$workerName]['path']);

        $workerClass = $this->workers[$workerName]['class'];
        $worker = new $workerClass($workerName);
        call_user_func_array(array($worker, 'init'), $arguments);
        call_user_func(array($worker, 'run'));
    }
}
